﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Timers;
using Common;
using LogLib;
using ComLib.ComType;

namespace Reader
{
    public class IDCReaderBase
    {
        /// <summary>
        /// 是否已被初始化
        /// </summary>
        protected bool _bInitialized = false;
        public bool Initialized
        {
            get { return _bInitialized; }
        }

        /// <summary>
        /// 同步操作标记值
        /// </summary>
        protected int syncPoint = 0;

        /// <summary>
        /// 阅读器选项
        /// </summary>
        protected IDCReaderOption _option = new IDCReaderOption();
        public IDCReaderOption Option
        {
            get { return _option; }
            set { _option = value; }
        }
 
        /// <summary>
        /// 阅读器连接的串口号（或者USB接口索引）
        /// </summary>
        protected int _iPort = 0;
        public int iPort
        {
            get { return _iPort; }
        }

        // 标记当前读卡器是否已经被占用
        protected bool _isBusy = false;
        public bool IsBusy
        {
            get { return _isBusy; }
        }

        /// <summary>
        /// 错误消息
        /// </summary>
        protected string _errorMsg;
        public string ErrorMessage
        {
            get
            {
                return _errorMsg;
            }
        }

        protected readonly string strZPPath = @"D:\\zp.bmp";

        protected System.Timers.Timer _timer;

        // 标记当前是否循坏读卡是否运行
        public bool Running
        {
            get
            {
                if (_timer != null)
                {
                    return _timer.Enabled;
                }
                return false;
            }
        }

        /// <summary>
        /// 卡片是否已取走
        /// </summary>        
        public virtual bool CardHasTokenAway
        {
            get
            {
                return true;
            }
        }

        public IDCReaderBase(IDCReaderOption option)
        {
            _iPort = option.SerialPortNum;
            _option = option;
        }

        public void SavePort()
        {
            _option.Save();
        }

        protected void Timer_Elapsed(object o, ElapsedEventArgs e)
        {
            if (!_bInitialized || _isBusy)
            {
                return;
            }
            Read(false);
        }

        protected void InitTimer()
        {
            _timer = new System.Timers.Timer();
            _timer.Interval = _option.ScanInterval;
            _timer.AutoReset = true;
            _timer.Elapsed += Timer_Elapsed;
        }

        public virtual bool InitSDK(int port)
        {
            return false;
        }

        public virtual bool ExitSDK()
        {
            return false;
        }

        public virtual void Read(bool initiative = false)
        {
            
        }

        /// <summary>
        /// 手动阅读身份证号码
        /// </summary>
        /// <param name="number">读到的号码</param>
        /// <returns>读取结果</returns>
        public virtual bool ReadManual(ref string number)
        {
            return false;
        }

        /// <summary>
        /// 清理身份证头像相片
        /// </summary>
        public void DeleteZP()
        {
            try
            {
                if (!File.Exists(strZPPath))
                {
                    File.Delete(strZPPath);
                }
            }
            catch (Exception ex)
            {
                string strLog = string.Format("删除ZP发生了异常: {0}", ex.ToString());
                Log.WriteException(strLog);
            }
        }

        public void Start()
        {
            if (_timer == null)
            {
                return;
            }
            if (!_timer.Enabled)
            {
                _timer.Start();            
            }
        }

        public void Stop()
        {
            if (_timer == null)
            {
                return;
            }
            if (_bInitialized || _timer.Enabled)
            {            
                _timer.Stop();
            }
        }
        /// <summary>
        /// 声明读卡器进入占用状态，阻断其它可用
        /// </summary>
        public void SetBusy()
        {
            _isBusy = true;
        }

        /// <summary>
        /// 解除读卡器的占用状态，重新设置为可用
        /// </summary>
        public void Reset()
        {
            _isBusy = false;         
        }
    }
}
